const { defineConfig } = require("@vue/cli-service");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["js", "css"];
const isProd = process.env.NODE_ENV === "production";
console.log("是否生产环境：", process.env.NODE_ENV, isProd);
const webpack = require("webpack");
const path = require("path");
const resolve = (dir) => {
  return path.join(__dirname, dir);
};
// CDN下载配置
const assetsCDN = {
  // webpack build externals
  externals: {
    // vue: "Vue",
    // "vue-router": "VueRouter",
    // vuex: "Vuex",
    // axios: "axios",
    // nprogress: "NProgress",
    // "ant-design-vue": "antd",
  },
  css: [
    // '//cdn.bootcdn.net/ajax/libs/ant-design-vue/1.7.8/antd.min.css'
  ],
  js: [
    // "//cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js",
    // "//cdn.bootcdn.net/ajax/libs/vue-router/3.4.3/vue-router.min.js",
    // "//cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js",
    // "//cdn.bootcdn.net/ajax/libs/ant-design-vue/1.7.8/antd.min.js",
    // '//cdn.bootcdn.net/ajax/libs/ant-design-vue/1.7.8/antd.js',
    // '//cdn.bootcdn.net/ajax/libs/ant-design-vue/1.7.8/antd-with-locales.min.js',
    // "//cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.js",
    // "//cdn.bootcdn.net/ajax/libs/vuex/3.6.2/vuex.min.js",
  ],
};
module.exports = defineConfig({
  /**
   * 开发反向代理
   */
  devServer: {
    port: 8080,
    proxy: {
      "/api/admin_server": {
        target: "http://127.0.0.1:10001/api/admin_server",
        ws: false,
        changeOrigin: true,
        cookieDomainRewrite: "*",
        pathRewrite: {
          "^/api/admin_server": "",
        },
      },
      "/api/front_server": {
        target: "http://127.0.0.1:10002/api/front_server",
        ws: false,
        changeOrigin: true,
        cookieDomainRewrite: "*",
        pathRewrite: {
          "^/api/front_server": "",
        },
      },
      "/api/job_server": {
        target: "http://127.0.0.1:10004/api/job_server",
        ws: false,
        changeOrigin: true,
        cookieDomainRewrite: "*",
        pathRewrite: {
          "^/api/job_server": "",
        },
      },
    },
  },
  // 避免构建后的代码中出现未转译的第三方依赖
  transpileDependencies: true,
  // 不需要source map,设置为false加速打包
  productionSourceMap: false,
  // 项目访问路径
  publicPath: "/",
  // 关闭检测
  lintOnSave: false,
  // 打包输出存放
  outputDir: "html",
  // 打包后静态资源存放
  assetsDir: "static",
  // 解决报错：正在使用Vue的仅运行时版本，并而模板编译器不可用，也可以使用包含编译器的内部版本
  runtimeCompiler: true,
  // 配置webpack
  configureWebpack: (config) => {
    // 压缩打包文件 *加快首屏访问速度
    config.plugins.push(
      new CompressionWebpackPlugin({
        algorithm: "gzip",
        test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
        threshold: 10240, // 大小超过10kb就压缩
        minRatio: 0.8,
      })
    );
    // 解决富文本vue-quill-editor引入报错问题
    config.plugins.push(
      new webpack.ProvidePlugin({
        // 在这儿添加下面两行
        "window.Quill": "quill/dist/quill.js",
        Quill: "quill/dist/quill.js",
      })
    );
    // 配置从CND下载的插件
    config.externals = isProd ? assetsCDN.externals : {};
  },
  // 打包配置
  chainWebpack: (config) => {
    if (isProd) {
      // 配置使用CDN
      config.plugin("html").tap((args) => {
        args[0].cdn = assetsCDN;
        return args;
      });
    }
    // 拆分chunk包 *加快首屏访问速度
    config.optimization.splitChunks({
      chunks: "all",
      cacheGroups: {
        // 打包项目其他依赖
        libs: {
          name: "libs",
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: "initial",
        },
        // UI框架单独打包
        antd: {
          name: "antd",
          // 量级必须大于 libs | app 否则会被打包成 libs | app
          priority: 20,
          test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/,
        },
        // 自定义组件单独打包
        commons: {
          name: "commons",
          test: resolve("src/components"),
          minChunks: 3,
          priority: 5,
          reuseExistingChunk: true,
        },
      },
    });
    config.optimization.runtimeChunk("single")
  },
});
